// eslint-disable-next-line node/no-missing-require
const vscode = require('vscode');
const { KEY_REG } = require('./defineConst');
const { joinPath } = require('./utils/index');
const unuseDecorationType = vscode.window.createTextEditorDecorationType({
  opacity: '0.6'
});

const textEditorTransview = vscode.window.onDidChangeActiveTextEditor((e) => {
  // const activeTextEditor = vscode.window.activeTextEditor;
  // console.log(activeTextEditor.document.getText());
  const text = e.document.getText();
  // console.log(KEY_REG.exec(text));
  let match = null
  const decorations = []
  console.log('00000000');
  let vueI18nConfig = null;
  // const fileStat = await vscode.workspace.fs.stat(vscode.Uri.parse(joinPath('vue.i18n.config.js')));
  // console.log(fileStat);
  try {
    vueI18nConfig = require(joinPath('vue.i18n.config.js'));
  } catch (error) {
    console.log('找不到 vue.i18n.config.js');
  }
  if(!vueI18nConfig){
    return;
  }

  console.log('vueI18nConfig', vueI18nConfig);

  while ((match = KEY_REG.exec(text))) {
    const index = match.index
    const matchKey = match[0];
    const keyStr = matchKey.replace(new RegExp(KEY_REG), '$1');
    console.log(keyStr);
    let mainText = vueI18nConfig.translate(keyStr) || 'not found';
    console.log(mainText);
    console.log(index);
    decorations.push({
      range: new vscode.Range(
        e.document.positionAt(index),
        e.document.positionAt(index + matchKey.length + 1)
      ),
      renderOptions: {
        after: {
          color: 'rgba(153, 153, 153, .7)',
          contentText: mainText ? `›${mainText}` : '',
          fontWeight: 'normal',
          fontStyle: 'normal'
        }
      }
    })
  }
  // KEY_REG.exec(text).forEach((item) => {
  // 	console.log(item);
  // 	console.log(item.replace(KEY_REG, '$1'));
  // });
  e.setDecorations(unuseDecorationType, decorations);
});


module.exports = textEditorTransview;